package cn.dankal.lequ.ui.activity;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.support.v7.app.AppCompatActivity;
import android.view.KeyEvent;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import java.util.Timer;
import java.util.TimerTask;

import butterknife.Bind;
import butterknife.ButterKnife;
import butterknife.OnClick;
import cn.dankal.lequ.R;
import cn.dankal.lequ.ui.fragment.ComprehensiveFragment;
import cn.dankal.lequ.ui.fragment.FindFragment;
import cn.dankal.lequ.ui.fragment.FunFragment;
import cn.dankal.lequ.ui.fragment.IndependentagameFragment;
import cn.dankal.lequ.ui.fragment.MineFragment;

/**
 * Created by 苏彬 on 2016/4/14.
 */
public class MainActivity extends AppCompatActivity {

    public static final int COMPREHENSIVE_FRAGMENT = 0;
    public static final int INDEPENDENTGAME_FRAGMENT = 1;
    public static final int FIND_FRAGMENT = 2;
    public static final int FUN_FRAGMENT = 3;
    public static final int MINE_FRAGMENT = 4;
    /**
     * 双击退出函数
     */
    private static Boolean isExit = false;
    @Bind(R.id.iv_bottom_navigation_1)
    ImageView img_comprehensive;
    @Bind(R.id.tv_bottom_navigation_1)
    TextView tv_comprehensive;
    @Bind(R.id.iv_bottom_navigation_2)
    ImageView img_independentagame;
    @Bind(R.id.tv_bottom_navigation_2)
    TextView tv_independentagame;
    @Bind(R.id.iv_bottom_navigation_3)
    ImageView img_find;
    @Bind(R.id.tv_bottom_navigation_3)
    TextView tv_find;
    @Bind(R.id.iv_bottom_navigation_4)
    ImageView img_fun;
    @Bind(R.id.tv_bottom_navigation_4)
    TextView tv_fun;
    @Bind(R.id.iv_bottom_navigation_5)
    ImageView img_mine;
    @Bind(R.id.tv_bottom_navigation_5)
    TextView tv_mine;
    private Fragment comprehensiveFragment, independentagameFragment, findFragment, funFragment, mineFragment;
    private int currentFragment = COMPREHENSIVE_FRAGMENT;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        ButterKnife.bind(this);
        addDefaultFragment();
    }

    @OnClick({R.id.rbl_bottom_navigation_1, R.id.rbl_bottom_navigation_2, R.id.rbl_bottom_navigation_3, R.id.rbl_bottom_navigation_4, R.id.rbl_bottom_navigation_5})
    public void onTabClick(View view) {
        FragmentManager fm = getSupportFragmentManager();
        FragmentTransaction ft = fm.beginTransaction();
        switch (view.getId()) {
            case R.id.rbl_bottom_navigation_1:
                ClickBottomNavigation(COMPREHENSIVE_FRAGMENT);
                if (currentFragment == COMPREHENSIVE_FRAGMENT)
                    return;
                ft.hide(getShowingFragment(currentFragment));
                ft.show(comprehensiveFragment);
                currentFragment = COMPREHENSIVE_FRAGMENT;
                break;
            case R.id.rbl_bottom_navigation_2:
                ClickBottomNavigation(INDEPENDENTGAME_FRAGMENT);
                if (independentagameFragment == null) {
                    independentagameFragment = new IndependentagameFragment();
                    ft.add(R.id.frame_content, independentagameFragment);
                }
                if (currentFragment == INDEPENDENTGAME_FRAGMENT)
                    return;
                ft.hide(getShowingFragment(currentFragment));
                ft.show(independentagameFragment);
                currentFragment = INDEPENDENTGAME_FRAGMENT;
                break;
            case R.id.rbl_bottom_navigation_3:
                ClickBottomNavigation(FIND_FRAGMENT);
                if (findFragment == null) {
                    findFragment = new FindFragment();
                    ft.add(R.id.frame_content, findFragment);
                }
                if (currentFragment == FIND_FRAGMENT)
                    return;
                ft.hide(getShowingFragment(currentFragment));
                ft.show(findFragment);
                currentFragment = FIND_FRAGMENT;
                break;
            case R.id.rbl_bottom_navigation_4:
                ClickBottomNavigation(FUN_FRAGMENT);
                if (funFragment == null) {
                    funFragment = new FunFragment();
                    ft.add(R.id.frame_content, funFragment);
                }
                if (currentFragment == FUN_FRAGMENT)
                    return;
                ft.hide(getShowingFragment(currentFragment));
                ft.show(funFragment);
                currentFragment = FUN_FRAGMENT;
                break;
            case R.id.rbl_bottom_navigation_5:
                ClickBottomNavigation(MINE_FRAGMENT);
                if (mineFragment == null) {
                    mineFragment = new MineFragment();
                    ft.add(R.id.frame_content, mineFragment);
                }
                if (currentFragment == MINE_FRAGMENT)
                    return;
                ft.hide(getShowingFragment(currentFragment));
                ft.show(mineFragment);
                currentFragment = MINE_FRAGMENT;
                break;
        }
        ft.commit();
    }

    private Fragment getShowingFragment(int currentFragment) {
        switch (currentFragment) {
            case COMPREHENSIVE_FRAGMENT:
                return comprehensiveFragment;
            case INDEPENDENTGAME_FRAGMENT:
                return independentagameFragment;
            case FIND_FRAGMENT:
                return findFragment;
            case FUN_FRAGMENT:
                return funFragment;
            case MINE_FRAGMENT:
                return mineFragment;
        }
        return null;
    }

    /**
     * 设置初始碎片布局
     */
    private void addDefaultFragment() {
        ClickBottomNavigation(COMPREHENSIVE_FRAGMENT);
        FragmentManager fm = getSupportFragmentManager();
        FragmentTransaction ft = fm.beginTransaction();
        comprehensiveFragment = new ComprehensiveFragment();
        ft.replace(R.id.frame_content, comprehensiveFragment);
        ft.commit();
    }

    /**
     * 菜单、返回键响应
     */
    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        // TODO Auto-generated method stub
        if (keyCode == KeyEvent.KEYCODE_BACK) {
            exitBy2Click(); //调用双击退出函数
        }
        return false;
    }

    public void exitBy2Click() {
        Timer tExit = null;
        if (isExit == false) {
            isExit = true; // 准备退出
            Toast.makeText(this, "再按一次退出程序", Toast.LENGTH_SHORT).show();
            tExit = new Timer();
            tExit.schedule(new TimerTask() {
                @Override
                public void run() {
                    isExit = false; // 取消退出
                }
            }, 2000); // 如果2秒钟内没有按下返回键，则启动定时器取消掉刚才执行的任务
        } else {
            finish();
            System.exit(0);
        }
    }

    /**
     * 底部导航栏item样式切换处理
     *
     * @param currentFragment
     */
    private void ClickBottomNavigation(int currentFragment) {
        //综合
        img_comprehensive.setImageResource(currentFragment == COMPREHENSIVE_FRAGMENT ?
                R.mipmap.homepage_comprehensive_input : R.mipmap.homepage_comprehensive);
        tv_comprehensive.setTextColor(currentFragment == COMPREHENSIVE_FRAGMENT ?
                getResources().getColor(R.color.colorAccent) : getResources().getColor(R.color.text_color));
        //独立游戏
        img_independentagame.setImageResource(currentFragment == INDEPENDENTGAME_FRAGMENT ?
                R.mipmap.homepage_independent_input : R.mipmap.homepage_independent);
        tv_independentagame.setTextColor(currentFragment == INDEPENDENTGAME_FRAGMENT ?
                getResources().getColor(R.color.colorAccent) : getResources().getColor(R.color.text_color));
        //发现
        img_find.setImageResource(currentFragment == FIND_FRAGMENT ?
                R.mipmap.homepage_find_input : R.mipmap.homepage_find);
        tv_find.setTextColor(currentFragment == FIND_FRAGMENT ?
                getResources().getColor(R.color.colorAccent) : getResources().getColor(R.color.text_color));
        //乐趣
        img_fun.setImageResource(currentFragment == FUN_FRAGMENT ?
                R.mipmap.homepage_pleasure_input : R.mipmap.homepage_pleasure);
        tv_fun.setTextColor(currentFragment == FUN_FRAGMENT ?
                getResources().getColor(R.color.colorAccent) : getResources().getColor(R.color.text_color));
        //我的
        img_mine.setImageResource(currentFragment == MINE_FRAGMENT ? R.mipmap.homepage_mime_input : R.mipmap.homepage_mime);
        tv_mine.setTextColor(currentFragment == MINE_FRAGMENT ?
                getResources().getColor(R.color.colorAccent) : getResources().getColor(R.color.text_color));
    }
}
